package com.slushpupie.deskclock;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.util.Log;
import android.R.string;

public class Prefs{
	private static final String LOG_TAG = "DeskClock";
	private int prefsKeepSreenOn = 0;
	private boolean prefsMilitaryTime = false;
	private boolean prefsShowMeridiem = false;
	private int prefsFontAlpha = 150;
	private int prefsFontColor = Color.WHITE;
	private int prefsBackgroundColor = Color.BLACK;
	private boolean prefsShowSeconds = false;
	private boolean prefsBlinkColon = false;
	private int prefsFont = 0;
	private int prefsScreenOrientation = -1;
	private boolean prefsScreenSaver = false;
	private boolean needsResizing = false;
	private Context context;
	public int getKeepSreenOn() {
		return prefsKeepSreenOn;
	}
	public void setKeepSreenOn(int prefsKeepSreenOn) {
		this.prefsKeepSreenOn = prefsKeepSreenOn;
	}
	public boolean isMilitaryTime() {
		return prefsMilitaryTime;
	}
	public void setMilitaryTime(boolean prefsMilitaryTime) {
		this.prefsMilitaryTime = prefsMilitaryTime;
	}
	public boolean isShowMeridiem() {
		return prefsShowMeridiem;
	}
	public void setShowMeridiem(boolean prefsShowMeridiem) {
		this.prefsShowMeridiem = prefsShowMeridiem;
	}
	public int getFontAlpha() {
		return prefsFontAlpha;
	}
	public void setFontAlpha(int prefsFontAlpha) {
		this.prefsFontAlpha = prefsFontAlpha;
	}
	public int getFontColor() {
		return prefsFontColor;
	}
	public void setFontColor(int prefsFontColor) {
		this.prefsFontColor = prefsFontColor;
	}
	public int getBackgroundColor() {
		return prefsBackgroundColor;
	}
	public void setBackgroundColor(int prefsBackgroundColor) {
		this.prefsBackgroundColor = prefsBackgroundColor;
	}
	public boolean isShowSeconds() {
		return prefsShowSeconds;
	}
	public void setShowSeconds(boolean prefsShowSeconds) {
		this.prefsShowSeconds = prefsShowSeconds;
	}
	public boolean isBlinkColon() {
		return prefsBlinkColon;
	}
	public void setBlinkColon(boolean prefsBlinkColon) {
		this.prefsBlinkColon = prefsBlinkColon;
	}
	public int getFont() {
		return prefsFont;
	}
	public void setFont(int prefsFont) {
		this.prefsFont = prefsFont;
	}
	public int getScreenOrientation() {
		return prefsScreenOrientation;
	}
	public void setScreenOrientation(int prefsScreenOrientation) {
		this.prefsScreenOrientation = prefsScreenOrientation;
	}
	public boolean isScreenSaver() {
		return prefsScreenSaver;
	}
	public void setScreenSaver(boolean prefsScreenSaver) {
		this.prefsScreenSaver = prefsScreenSaver;
	}
	/*
	public boolean isNeedResize(){
		return needsResizing;
	}
	*/
	public Prefs(SharedPreferences prefs, Context context){
		if(prefs == null){
			throw new NullPointerException();
		}

		if(context == null){
			throw new NullPointerException();
		}

		this.context = context;
		this.needsResizing = loadPrefs(prefs);
	}
	
	public boolean loadPrefs(SharedPreferences prefs) {
		Log.d(LOG_TAG, "loading preferences");

		try {
			prefsKeepSreenOn = Integer.valueOf(prefs.getString(
					"pref_keep_screen_on", "0"));
		} catch (NumberFormatException e) {
			prefsKeepSreenOn = 0;
		}
		

		String pso = prefs.getString("pref_screen_orientation", "auto");
		if ("portrait".equals(pso)) {
			prefsScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
		} else if ("landscape".equals(pso)) {
			prefsScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
		} else {
			prefsScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
		}

		prefsMilitaryTime = prefs.getBoolean("pref_military_time", false);

		boolean showMeridiem = prefs.getBoolean("pref_meridiem", false);
		if (showMeridiem != prefsShowMeridiem) {
			prefsShowMeridiem = showMeridiem;
			needsResizing = true;
		}

		try {
			prefsFontAlpha = Integer.valueOf(prefs.getString("pref_alpha", "150"));
		} catch (NumberFormatException e) {
			prefsFontAlpha = 150;
		}
		
		try {
			prefsFontColor = prefs.getInt("pref_color", Color.WHITE);
		} catch (NumberFormatException e) {
			prefsFontColor = Color.WHITE;
		}
		

		try {
			prefsBackgroundColor = prefs.getInt("pref_background_color",
					Color.BLACK);
		} catch (NumberFormatException e) {
			prefsBackgroundColor = Color.BLACK;
		}

		boolean showSeconds = prefs.getBoolean("pref_show_seconds", false);
		if (prefsShowSeconds != showSeconds) {
			prefsShowSeconds = showSeconds;
			needsResizing = true;
		}

		prefsBlinkColon = prefs.getBoolean("pref_blink_seconds", false);

		try {
			int n = Integer.valueOf(prefs.getString("pref_font",
					context.getString(R.string.pref_default_font)));
			if (n != prefsFont) {
				prefsFont = n;
				needsResizing = true;
			}
		} catch (NumberFormatException e) {
			if (prefsFont != Integer
					.valueOf(context.getString(R.string.pref_default_font))) {
				prefsFont = Integer
						.valueOf(context.getString(R.string.pref_default_font));
				needsResizing = true;
			}
		}

		boolean ss = prefs.getBoolean("pref_screensaver", false);
		if (ss != prefsScreenSaver) {
			prefsScreenSaver = ss;
			needsResizing = true;
		}
		
		return needsResizing;
	}
}